package ch.fusun.baron.swt.isometry.components;

import org.eclipse.swt.graphics.Image;

/**
 * A sprite defines the image and sensitive mask of a graphical item (and
 * potentially rotated images)
 */
public class Sprite {
	private final Image image;
	private final Image image90;
	private final Image image180;
	private final Image image270;
	private Image mask;
	private Image halfTransparentMask;
	private int priority = 0;

	/**
	 * Constructor
	 * 
	 * @param image
	 * @param mask
	 */
	public Sprite(Image image, Image mask) {
		this(image);
		this.mask = mask;
		if (mask != null) {
			this.halfTransparentMask = halfTransparent(this.mask);
		}
	}

	/**
	 * @param image
	 *            The image of the sprite
	 */
	public Sprite(Image image) {
		if (image == null) {
			throw new RuntimeException("Image can not be null"); //$NON-NLS-1$
		}
		this.image = image;
		this.image90 = image;
		this.image180 = image;
		this.image270 = image;
	}

	/**
	 * @param image
	 *            The image of the sprite
	 * @param image90
	 * @param image180
	 * @param image270
	 * @param priority
	 */
	public Sprite(Image image, Image image90, Image image180, Image image270,
			int priority) {
		if (image == null || image90 == null || image180 == null
				|| image270 == null) {
			throw new RuntimeException("Image can not be null"); //$NON-NLS-1$
		}
		this.image = image;
		this.image90 = image90;
		this.image180 = image180;
		this.image270 = image270;
		this.setPriority(priority);
	}

	/**
	 * @param image
	 *            The image
	 * @param mask
	 *            The mask (black&white)
	 * @param priority
	 *            The priority
	 */
	public Sprite(Image image, Image mask, int priority) {
		this(image, mask);
		this.setPriority(priority);
	}

	/**
	 * Without mask (not clickable)
	 * 
	 * @param image
	 *            The image
	 * @param priority
	 *            The priority
	 */
	public Sprite(Image image, int priority) {
		this(image);
		this.setPriority(priority);
	}

	/**
	 * @return The image
	 */
	public Image getImage() {
		return image;
	}

	/**
	 * @return 90� rotated image
	 */
	public Image getImage90() {
		return image90;
	}

	/**
	 * @return 180� rotated image
	 */
	public Image getImage180() {
		return image180;
	}

	/**
	 * @return 270� rotated image
	 */
	public Image getImage270() {
		return image270;
	}

	/**
	 * @return The mask
	 */
	public Image getMask() {
		return mask;
	}

	/**
	 * @param i
	 *            The i position
	 * @param j
	 *            The j position
	 * @return Is the mask sensitive at those pixel coordinates?
	 */
	public boolean isSensitive(int i, int j) {
		return mask.getImageData().getAlpha(i, j) > 0;
	}

	/**
	 * This just returns the mask right now
	 * 
	 * @return a half transparent mask
	 */
	@Deprecated
	public Image getHalfTransparentMask() {
		return halfTransparentMask;
	}

	/**
	 * @param anImage
	 *            The source image
	 * @return The image half transparented
	 */
	@Deprecated
	protected Image halfTransparent(Image anImage) {
		// ImageData imageData = new ImageData(anImage.getImageData().width,
		// anImage.getImageData().width, anImage.getImageData().depth,
		// anImage.getImageData().palette);
		// ImageData anImageData = anImage.getImageData();
		// for (int i = 0; i < imageData.width; i++) {
		// for (int j = 0; j < imageData.height; j++) {
		// if (anImageData.getPixel(i, j) > 0) {
		// imageData.setPixel(i, j, anImageData.getPixel(i, j));
		// imageData.setAlpha(i, j, 100);
		// }
		// }
		// }
		// return new Image(anImage.getDevice(), imageData);
		return anImage;
	}

	/**
	 * @param priority
	 *            The priority
	 */
	public void setPriority(int priority) {
		this.priority = priority;
	}

	/**
	 * @return The priority
	 */
	public int getPriority() {
		return priority;
	}

	/**
	 * Dispoes the mask and the image
	 */
	public void dispose() {
		this.image.dispose();
		if (this.mask != null) {
			mask.dispose();
		}
	}
}
